﻿@page "/listener"
@page "/listener/index"
@attribute [Authorize(Roles = "User, Administrator")]
@inherits OwningComponentBase<ICovenantService>
@implements IDisposable

@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components.Authorization

@using Covenant.Core
@using Covenant.Models.Listeners
@using Covenant.Components.Profiles
@inject IJSRuntime IJSRuntime
@inject INotificationService INotificationService
@inject AuthenticationStateProvider AuthenticationStateProvider

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3">
    <h1 class="h2">Listeners</h1>
</div>

<ul class="nav nav-tabs mb-3" id="listener-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link" id="listeners-tab" data-toggle="tab" href="#listeners" role="tab" aria-controls="listeners" aria-selected="false">
            <span class="fe fe-headphones"></span>
            Listeners
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="profiles-tab" data-toggle="tab" href="#profiles" role="tab" aria-controls="profiles" aria-selected="false">
            <span class="fe fe-settings"></span>
            Profiles
        </a>
    </li>
</ul>

<div class="tab-content" id="listeners-tab-content">
    <div class="tab-pane fade" id="listeners" role="tabpanel" aria-labelledby="listeners-tab">
        <ListenerTable @bind-Listeners="Listeners" />
        <a href="/listener/create" class="btn btn-primary">
            <span class="fe fe-plus"></span>
            Create
        </a>
    </div>
    <div class="tab-pane fade" id="profiles" role="tabpanel" aria-labelledby="profiles-tab">
        <ProfileTable Profiles="Profiles" />
        @if (IsAdmin())
        {
            <a href="/profile/create" class="btn btn-primary">
                <span class="fe fe-plus"></span>
                Create
            </a>
        }
    </div>
</div>

@code {
    [Parameter]
    public List<Listener> Listeners { get; set; }

    [Parameter]
    public IEnumerable<Profile> Profiles { get; set; }

    protected override async Task OnInitializedAsync()
    {
        this.Listeners = this.Listeners ?? (await Service.GetListeners()).ToList();
        this.Profiles = this.Profiles ?? await Service.GetProfiles();
        Service.DisposeContext();
        this.INotificationService.OnCreateListener += OnCreateListener;
        this.INotificationService.OnEditListener += OnEditListener;
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await IJSRuntime.InvokeAsync<string>("ShowTab", "#listeners-tab");
        }
    }

    public void Dispose()
    {
        this.INotificationService.OnCreateListener -= OnCreateListener;
        this.INotificationService.OnEditListener -= OnEditListener;
    }

    private void OnCreateListener(object sender, Listener listener)
    {
        this.Listeners.Add(Service.GetListener(listener.Id).WaitResult());
        Service.DisposeContext();
        this.InvokeAsync(() => this.StateHasChanged());
    }

    private void OnEditListener(object sender, Listener listener)
    {
        if (this.Listeners.Any(L => L.Id == listener.Id))
        {
            this.Listeners[this.Listeners.FindIndex(L => L.Id == listener.Id)] = Service.GetListener(listener.Id).WaitResult();
            Service.DisposeContext();
            this.InvokeAsync(() => this.StateHasChanged());
        }
    }

    private bool IsAdmin()
    {
        return AuthenticationStateProvider
            .GetAuthenticationStateAsync()
            .WaitResult().User
            .IsInRole("Administrator");
    }
}
